package com.kaibes.module.jwt.local;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.kaibes.module.account.pojo.Account;
import com.kaibes.module.jwt.service.AccountService;

@Service
public class AccountService4Jwt implements AccountService {

    @Autowired
    private com.kaibes.module.account.core.AccountService service;
    @Autowired
    private PasswordEncoder passwordEncoder;
    private ThreadLocal<Account> local = new ThreadLocal<>();
    
    @Override
    public boolean checkAccount(String username) {
        Account account = service.getByUsername(username);
        
        local.set(account);
        return account != null;
    }

    @Override
    public boolean checkPassword(String password) {
        Account account = local.get();
        if (account == null) {
            return false;
        }
        return passwordEncoder.matches(password, account.getPassword());
    }

    @Override
    public Integer getUserId() {
        Account account = local.get();
        if (account == null) {
            return null;
        }
        return account.getUserId();
    }

}
